# Maintainer: Oscar Fuentes <ofv@wanadoo.es>

_realname=tclx
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_majorminor=8.4
pkgver=${_majorminor}.4
pkgrel=1
pkgdesc="Extends Tcl by providing new operating system interface commands, extended file control and many other (mingw-w64)"
arch=('any')
url="https://tclx.sourceforge.io/"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-tcl")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/flightaware/tclx/archive/v${pkgver}.tar.gz"
        '001-fix-detect-mingw.patch'
        '002-fix-func-declaration.patch'
        'random.c')
sha256sums=('44d834d0996e9a7bbaf22d85d9dc17eac2ccc43ab0243a21087e3944c7bc3825'
            '5018594a4889c41fcb1a669f29b971dff19759c0fb2d8939bb495bb3715dbf08'
            '1f7d6a6c6fa7d3535ff57cac9701bdc8a156bd0f6b6d3e9da69c91c3b9bb8fea'
            '32b1b0685d67f20d8f7943682d16caaa86915937f745471652835239af51750c')

prepare() {
  local dst=${srcdir}/${_realname}-${pkgver}/compat
  mkdir -p ${dst}
  cp random.c ${dst}

  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-fix-detect-mingw.patch
  patch -p1 -i ${srcdir}/002-fix-func-declaration.patch
}

build() {
  mkdir -p "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"

  [[ "${MINGW_CHOST}" = 'x86_64-w64-mingw32' ]] && enable64bit='--enable-64bit'

  CFLAGS="-DUSE_INTERP_ERRORLINE $CFLAGS" \
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    $enable64bit
  make
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install

  mv ${pkgdir}${MINGW_PREFIX}/lib/tclx8.6/*.dll ${pkgdir}${MINGW_PREFIX}/bin/
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/license.terms ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/license.terms
}
